class Solution {
    public:
        vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
            ranges::sort(nums1);
            ranges::sort(nums2);
            vector<int> ans;
            int i = 0, j = 0;
            while (i < nums1.size() && j < nums2.size()) {
                int x = nums1[i], y = nums2[j];
                if (x < y) {
                    i++;
                } else if (x > y) {
                    j++;
                } else {
                    ans.push_back(x);
                    i++;
                    j++;
                }
            }
            return ans;
        }
    };